<template>
  <!-- 余额管理 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item prop="nickName">
        <el-input v-model="queryParams.userWallet" placeholder="余额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table v-loading="loading" border :data="dataList">
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户电话号码" align="center" prop="phonenumber" />
      <el-table-column label="余额" align="center" prop="userWallet" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleReduce(scope.row)">扣减余额</el-button>
          <el-button type="text" size="mini" @click="handleAdd(scope.row)">充值余额</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页  -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize" @pagination="getList" />

    <!-- 添加余额 -->
    <el-dialog title="添加余额" :visible.sync="addShow" width="40%" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" label-width="100px" :rules="rules">
        <el-form-item label="增加金额" prop="money">
          <el-input v-model="addForm.money"></el-input>
        </el-form-item>
        <el-form-item label="订单号" prop="sn">
          <el-input v-model="addForm.sn"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="addHandleSure">确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 扣减余额 -->
    <el-dialog title="扣减余额" :visible.sync="reduceShow" width="40%" :close-on-click-modal="false">
      <el-form ref="reduceForm" :model="reduceForm" label-width="120px" :rules="reduceRules">
        <el-form-item label="扣减金额" prop="money">
          <el-input v-model="reduceForm.money"></el-input>
        </el-form-item>
        <el-form-item label="订单号" prop="sn">
          <el-input v-model="reduceForm.sn"></el-input>
        </el-form-item>
        <el-form-item label="购买产品和期限" prop="term">
          <el-input v-model="reduceForm.term"></el-input>
        </el-form-item>
        <el-form-item label="产品类型名称" prop="productTypeName">
          <el-input v-model="reduceForm.productTypeName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="reduceHandleSure">确 定</el-button>
        <el-button @click="reduceCancelForm">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { userWalletList, updateUserWallet } from "@/api/financialManagement/balanceManagement";

export default {
  data() {
    return {
      loading: false, // 遮罩层
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      dataList: [], // 充值管理表格数据
      // 查询参数
      queryParams: {
        nickName: null,
        params: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      addShow: false,//添加弹窗开关
      addForm: {},//添加的内容
      rules: {//添加表单验证
        money: [
          { required: true, message: '请输入金额', trigger: 'change' }
        ],
        sn: [
          { required: true, message: '请输入订单编号', trigger: 'change' }
        ],
      },
      reduceShow: false,//扣减弹窗开关
      reduceForm: {},//扣减表单数据
      reduceRules: {
        money: [
          { required: true, message: '请输入金额', trigger: 'change' }
        ],
        sn: [
          { required: true, message: '请输入订单编号', trigger: 'change' }
        ],
        term: [
          { required: true, message: '请输入购买产品和期限', trigger: 'change' }
        ],
        productTypeName: [
          { required: true, message: '请输入产品类型名称', trigger: 'change' }
        ],
      },
    };
  },
  created() {
    //获取表格数据
    this.getList();
  },
  methods: {
    /**打开扣减余额弹窗 */
    handleReduce(row) {
      this.reduceShow = true;
      this.reduceForm = Object.assign({}, row);
    },
    /**确认扣减余额 */
    reduceHandleSure() {
      this.$refs['reduceForm'].validate((valid) => {
        if (valid) {
          updateUserWallet( 'decrease' , this.reduceForm).then(res => {
            if (200 == res.code) {
              this.getList();
              this.reduceShow = false;
              this.$message.success('余额扣减成功！');
            }
          })
        }
      });
    },
    /**关闭扣减余额对话框 */
    reduceCancelForm() {
      this.resetForm('reduceForm');
      this.reduceShow = false;
    },
    /**打开添加余额对话框 */
    handleAdd(row) {
      this.addShow = true;
      this.addForm = Object.assign({}, row);
    },
    /**确认添加余额 */
    addHandleSure() {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          updateUserWallet( 'increase' , this.addForm).then(res => {
            if (200 == res.code) {
              this.getList();
              this.addShow = false;
              this.$message.success('余额增加成功！');
            }
          })
        }
      });
    },
    /**关闭添加余额 */
    cancelForm() {
      this.resetForm('addForm');
      this.addShow = false;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      userWalletList(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>

<style lang="scss" scoped>
.add-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.flex {
  display: flex;

  >div {
    flex: 1;
  }
}

.el-select {
  width: 100%;
}
</style>
